package com.ninth_group.dao.Impl;

import com.ninth_group.dao.Forecast7DDao;
import com.ninth_group.function.TriFunction;
import com.ninth_group.po.Forecast7D;
import com.ninth_group.utils.QueryUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class Forecast7DDaoImpl implements Forecast7DDao {
    @Autowired
    private QueryUtil queryUtil;

    private static final String TABLE_NAME = "forecast_7days";

    /**
     * 根据城市和省份名称获取
     *
     * @param cityIds  城市id集合
     * @return 城市基本信息
     */
    @Override
    public List<Forecast7D> getCity7DaysData(List<String> cityIds) {
        return getCity7DaysData(
                TABLE_NAME,
                cityIds,
                Forecast7D.class,
                null,
                QueryUtil::getCityByIdToList
        );
    }

    /**
     * 根据城市和省份名称获取城市24小时天气信息
     *
     * @param cityIds 城市id集合
     * @param getCityIdFunc 获取城市代码的函数
     * @return 城市基本信息
     */
    public <T> List<T> getCity7DaysData(String tableName,
                                        List<String> cityIds,
                                        Class<T> clazz,
                                        TriFunction<String, String, Class<T>, T> getCityIdFunc,
                                        TriFunction<String, String, Class<T>, List<T>> multiQueryFunc
    ) {
        Object cityData = queryUtil.getCityData(
                tableName,
                cityIds,
                clazz,
                getCityIdFunc,
                multiQueryFunc
        );

        if (cityData == null) {
            return null;
        }

        return (List<T>) cityData;
    }
}
